package com.pengbei.enshrine.ui.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.pengbei.enshrine.R;
import com.pengbei.enshrine.base.BaseActivity;
import com.pengbei.enshrine.common.utils.CheckUtils;
import com.pengbei.enshrine.common.utils.PreferencesUtils;
import com.pengbei.enshrine.common.utils.StatusBarUtils;
import com.pengbei.enshrine.network.bean.WelecomBannerModel;
import com.pengbei.enshrine.ui.main.MainActivity;
import com.youth.banner.Banner;
import com.youth.banner.indicator.RectangleIndicator;
import com.youth.banner.listener.OnPageChangeListener;

import java.util.ArrayList;
import java.util.List;

public class WelecomActivity extends BaseActivity implements View.OnClickListener {

    private ImageView imgWelecomBack;
    private TextView tvWelecomSkip;
    private Banner welecomBanner;
    private RectangleIndicator welecomIndicator;
    private TextView tvWelecomButton;
    private List<WelecomBannerModel> bannerlist = new ArrayList<>();
    private int type = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.StatusBarTransparentBgBlackText(WelecomActivity.this);
        setContentView(R.layout.activity_welecom);
        welecomdata();
        initView();
    }

    private void initView() {
        imgWelecomBack = (ImageView) findViewById(R.id.img_welecom_back);
        tvWelecomSkip = (TextView) findViewById(R.id.tv_welecom_skip);
        welecomBanner = (Banner) findViewById(R.id.welecom_banner);
        welecomIndicator = (RectangleIndicator) findViewById(R.id.welecom_indicator);
        tvWelecomButton = (TextView) findViewById(R.id.tv_welecom_ok);

        welecomBanner
                .setAdapter(new WelecomBannerAdapter(WelecomActivity.this,bannerlist))
                .addBannerLifecycleObserver(WelecomActivity.this)
                .isAutoLoop(false)
                .setIndicator(welecomIndicator,false)
                .setIndicatorSpace(4)
                .setIndicatorHeight(15)
                .setIndicatorNormalWidth(15)
                .setIndicatorSelectedWidth(36)
                .setIndicatorNormalColor(getResources().getColor(R.color.color_ACB5BD))
                .setIndicatorSelectedColor(getResources().getColor(R.color.color_8001FE));

        welecomBanner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                type = position;
                switch (position){
                    case 0:
                        imgWelecomBack.setVisibility(View.GONE);
                        tvWelecomButton.setText(getResources().getString(R.string.lc_welecomsubbutton0));
                        break;
                    case 1:
                        imgWelecomBack.setVisibility(View.VISIBLE);
                        tvWelecomButton.setText(getResources().getString(R.string.lc_welecomsubbutton1));
                        break;
                    case 2:
                        imgWelecomBack.setVisibility(View.VISIBLE);
                        tvWelecomButton.setText(getResources().getString(R.string.lc_welecomsubbutton2));
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        tvWelecomButton.setOnClickListener(this);
        imgWelecomBack.setOnClickListener(this);
        tvWelecomSkip.setOnClickListener(this);
        tvWelecomButton.setText(getResources().getString(R.string.lc_welecomsubbutton0));

    }


    @Override
    public void onClick(View v) {
        if (!CheckUtils.isFastDoubleClick()){
            switch (v.getId()){
                case R.id.img_welecom_back://返回
                    if(type!=0){
                        type--;
                    }
                    welecomBanner.setCurrentItem(type);
                    break;
                case R.id.tv_welecom_skip://跳过
                    goMain();
                    break;
                case R.id.tv_welecom_ok://底部按钮
                    switch (type){
                        case 0://
                            welecomBanner.setCurrentItem(1);
                            break;
                        case 1://下一步
                            welecomBanner.setCurrentItem(2);
                            break;
                        case 2://开始
                            goMain();
                            break;
                    }
                    break;
            }
        }
    }


    private void welecomdata(){
        WelecomBannerModel model0=new WelecomBannerModel();
        model0.setType("0");
        model0.setTitle(getResources().getString(R.string.lc_welecomtitle0));
        model0.setSubtitle(getResources().getString(R.string.lc_welecomsubtitle0));
        bannerlist.add(model0);

        WelecomBannerModel model1=new WelecomBannerModel();
        model1.setType("1");
        model1.setTitle(getResources().getString(R.string.lc_welecomtitle1));
        model1.setSubtitle(getResources().getString(R.string.lc_welecomsubtitle1));
        bannerlist.add(model1);

        WelecomBannerModel model2=new WelecomBannerModel();
        model2.setType("2");
        model2.setTitle(getResources().getString(R.string.lc_welecomtitle2));
        model2.setSubtitle(getResources().getString(R.string.lc_welecomsubtitle2));
        bannerlist.add(model2);
    }

    private void goMain(){
        PreferencesUtils.putString(WelecomActivity.this,"showwelecom","comeby");
        MainActivity.goToThisActivity(WelecomActivity.this);
        finish();
    }

    public static void goToThisActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, WelecomActivity.class);
        context.startActivity(intent);
    }


}
